package com.papper.mng.controller.biz;

import com.papper.common.bean.biz.BizDatasourceConfig;
import com.papper.common.bean.biz.BizTemplateConfig;
import com.papper.common.bean.biz.BizTemplateGroup;
import com.papper.common.config.SysConfigHelp;
import com.papper.common.domain.TableColumn;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.biz.BizDatasourceConfigService;
import com.papper.common.service.biz.BizTemplateConfigService;
import com.papper.common.service.biz.BizTemplateGroupService;
import com.papper.common.service.tripartite.CodeGenService;
import com.papper.common.service.tripartite.JDBCService;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.Validate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:LaJiao
 * @Date:2022-03-26 18:50:40
 * @Description:代码生成
 */
@Controller
@RequestMapping("/code/gen")
public class CodeGenController extends BaseAction {

    @Autowired
    JDBCService                jdbcService;
    @Autowired
    BizDatasourceConfigService bizDatasourceConfigService;
    @Autowired
    BizTemplateGroupService    bizTemplateGroupService;
    @Autowired
    BizTemplateConfigService   bizTemplateConfigService;
    @Autowired
    CodeGenService             codeGenService;

    /**
     * 代码生成
     *
     * @return
     */
    @GetMapping(value = "")
    public String execute(HttpServletRequest _request, Model model, Long dId, Long gId) {
        // 获取数据表列表
        List<BizDatasourceConfig> datasourceConfigs = bizDatasourceConfigService.findAll();
        model.addAttribute("datasourceConfigs", datasourceConfigs);
        BizDatasourceConfig bizDatasourceConfig = bizDatasourceConfigService.findById(dId);
        if (bizDatasourceConfig == null && !datasourceConfigs.isEmpty()) {
            bizDatasourceConfig = datasourceConfigs.get(0);
            dId = bizDatasourceConfig.getId();
        }
        model.addAttribute("bizDatasourceConfig", bizDatasourceConfig);
        model.addAttribute("dId", dId);
        if (bizDatasourceConfig != null) {
            List<TableColumn> tableColumns = jdbcService.mysqlTable(bizDatasourceConfig.getDriverClass(), bizDatasourceConfig.getHost(), bizDatasourceConfig.getUsername(), bizDatasourceConfig.getPassword(), bizDatasourceConfig.getDbName());
            model.addAttribute("tableColumns", tableColumns);
        }
        // 获取模板列表
        List<BizTemplateGroup> bizTemplateGroups = bizTemplateGroupService.findAll();
        model.addAttribute("bizTemplateGroups", bizTemplateGroups);
        BizTemplateGroup bizTemplateGroup = bizTemplateGroupService.findById(gId);
        if (bizTemplateGroup == null && !bizTemplateGroups.isEmpty()) {
            bizTemplateGroup = bizTemplateGroups.get(0);
            gId = bizTemplateGroup.getId();
        }
        model.addAttribute("bizTemplateGroup", bizTemplateGroup);
        model.addAttribute("gId", gId);
        if (bizTemplateGroup != null) {
            List<BizTemplateConfig> templateConfigs = bizTemplateConfigService.findByGroup(bizTemplateGroup.getId());
            model.addAttribute("templateConfigs", templateConfigs);
        }
        // 作者信息
        String CodeGenOutPath = SysConfigHelp.I().TGS("SystemConfig", "CodeGenOutPath", "E://CodeGen/AutomaticCodeGeneration/");
        String CodeGenAuth    = SysConfigHelp.I().TGS("SystemConfig", "CodeGenAuth", "LaJiao");
        String BasePackage    = SysConfigHelp.I().TGS("SystemConfig", "BasePackage", "com.papper");
        model.addAttribute("CodeGenOutPath", CodeGenOutPath);
        model.addAttribute("CodeGenAuth", CodeGenAuth);
        model.addAttribute("BasePackage", BasePackage);
        return render("/code/gen/index");
    }

    @PostMapping(value = "")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, Long dId, Long gId, String outPath, String basePackage, String sysAuthor, String tables, String temps) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }

        // 数据校验
        Validate.notNull(dId, "数据源信息为空");
        Validate.notNull(gId, "模板组信息为空");
        Validate.notEmpty(outPath, "输出文件信息不能为空!");
        Validate.notEmpty(basePackage, "基础包信息为空!");
        Validate.notEmpty(sysAuthor, "作者信息不能为空!");
        Validate.notEmpty(tables, "请选中生成代码数据表信息!");
        Validate.notEmpty(temps, "请选中生成代码模板配置信息!");

        // 添加模板组表
        BizDatasourceConfig bizDatasourceConfig = bizDatasourceConfigService.findById(dId);
        BizTemplateGroup    bizTemplateGroup    = bizTemplateGroupService.findById(gId);
        codeGenService.codeGen(bizDatasourceConfig, bizTemplateGroup, outPath, basePackage, sysAuthor, tables, temps);

        return new DwzResponse("200", "代码生成成功!!!", "closeCurrent");
    }
}



